import collections


class Solution(object):
    def bestCoordinate(self, towers, radius):
        d = collections.defaultdict(int)
        for x, y, q in towers:
            if q > 0:
                for ux in range(x - radius, x + radius + 1):
                    for uy in range(y - radius, y + radius + 1):
                        r = (ux - x) ** 2 + (uy - y) ** 2
                        if r <= radius ** 2:
                            d[(ux, uy)] += int(q / (1 + r ** 0.5))
        if not d:
            return [0, 0]
        max_val = -float('inf')
        for p in d:
            if d[p] > max_val:
                max_val = d[p]
                max_point = p
            else:
                if d[p] == max_val:
                    max_point = min(p, max_point)
        return list(max_point)
